package com.project.nimsen.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Finder;
import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.ProfitDto;
import com.project.nimsen.service.IProfitService;
@Repository("profitService")
public class ProfitServiceImpl extends BaseServiceImpl implements
		IProfitService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public List<Map<String, Object>> findUserByDto(ProfitDto dto)
			throws Exception {
		if(dto==null||dto.getPage()==null)
			return null;
		Finder finder = new Finder("select tm.* from T_MEMBER tm where tm.ACTIVE!=1");
		if(StringUtils.isNotBlank(dto.getName())){
			finder.append(" and tm.M_TRUE_NAME like :name");
			finder.setParam("name", "%"+dto.getName()+"%");
		}
		if("1".equals(dto.getCkb_dk())){
			finder.append(" and ID in (SELECT P_USER_ID from T_PROFIT WHERE BACK_INFO=1)");
		}
		finder.append(" ORDER BY tm.CREATE_TIME DESC");
		List<Map<String, Object>> rows = baseDao.queryForList(finder, dto.getPage());
		if(CollectionUtils.isEmpty(rows))
			return null;
		for(Map<String, Object> row : rows){
			Finder m_finder = new Finder("select sum(MONEYS) from T_PROFIT where "
				+ "BACK_INFO=1 and P_USER_ID=:puserid");
			m_finder.setParam("puserid", row.get("ID").toString());
			Object money = baseDao.queryForObject(m_finder, Object.class);
			if(money!=null)
				row.put("MONEYS",money);
			Object obj_date = row.get("CREATE_TIME");
			if(obj_date!=null){
				Date create_time = (Date)obj_date;
				row.put("CREATE_TIME", DateUtils.convertDate2String(DateUtils.DEFAILT_DATE_TIME_PATTERN, create_time));
			}
		}
		return rows;
	}
	public List<Map<String, Object>> findProByDto(ProfitDto dto)
			throws Exception {
		if(dto==null||StringUtils.isBlank(dto.getUserid())){
			return null;
		}
		Finder finder = new Finder("SELECT tp.*, tm.M_TRUE_NAME as FROM_USER_NAME, ssu.TRUE_NAME as SH_NAME, "
			+ "dsu.TRUE_NAME as DK_NAME from T_PROFIT tp LEFT JOIN T_MEMBER tm on tp.S_USER_ID=tm.ID LEFT JOIN"
			+ " S_USER ssu on tp.USER_ID_S=ssu.ID LEFT JOIN S_USER dsu on tp.D_USER_ID=dsu.ID where tm.ACTIVE!=1"
			+ " and tp.P_USER_ID=:puserid");
		finder.setParam("puserid", dto.getUserid());
		if(StringUtils.isNotBlank(dto.getBack_info())){
			finder.append(" and tp.BACK_INFO=:back_info");
			finder.setParam("back_info", dto.getBack_info());
		}
		List<Map<String, Object>> rows = baseDao.queryForList(finder, dto.getPage());
		if(CollectionUtils.isEmpty(rows))
			return null;
		for(Map<String, Object> row : rows){
			//反利时间
			Object obj_date = row.get("CREATE_TIME");
			if(obj_date!=null){
				Date time = (Date)obj_date;
				row.put("CREATE_TIME", DateUtils.convertDate2String(DateUtils.DEFAILT_DATE_TIME_PATTERN, time));
			}
			//打款时间
			Object obj_d_time = row.get("D_TIME");
			if(obj_d_time!=null){
				Date time = (Date)obj_d_time;
				row.put("time", DateUtils.convertDate2String(DateUtils.DEFAILT_DATE_TIME_PATTERN, time));
			}
			// 反还情况
			Object back_info = row.get("BACK_INFO");
			if("1".equals(back_info.toString())){
				row.put("BACK_INFO", "未反");
			}else{
				row.put("BACK_INFO", "已反");
			}
		}
		return rows;
	}
	public boolean updateDKByIds(String ids) throws Exception {
		String [] arr_str = ids.split(",");
		List<String> list_strs = new ArrayList<String>();
		for(String str : arr_str){
			list_strs.add(str);
		}
		Date now = new Date();
		Finder finder = new Finder("update T_PROFIT set BACK_INFO=2, D_TIME=:now where id in ( :idsw )");
		finder.setParam("now", now);
		finder.setParam("idsw", list_strs);
		return baseDao.update(finder)>0;
	}
}
